#include "client.h"

void*threadFunc_puts(void*arg){
    thread_t*thread=(thread_t*)arg;
    //用新的netfd连接客户端
    int new_sockfd=connectTcp(thread->ip,thread->port);
    
    train_t cmds;
    cmds.type=USER_TOKEN;
    cmds.length=strlen(thread->username);
    strcpy(cmds.data,thread->username);
    //发送用户名
    send(new_sockfd,&cmds,sizeof(cmds.type)+sizeof(cmds.length)+cmds.length,0);

    bzero(&cmds, sizeof(train_t));
    cmds.type=USER_TOKEN;
    cmds.length=strlen(thread->token);
    strcpy(cmds.data,thread->token);
    //发送token验证
    send(new_sockfd,&cmds,sizeof(cmds.type)+sizeof(cmds.length)+cmds.length,0);
    
    //发送用户登录时用netfd
    send(new_sockfd,&thread->netfd,sizeof(int),0);
    //发送下载或上传命令
    bzero(&cmds, sizeof(train_t));
    cmds.type=CMD_PUTS;
    strcpy(cmds.data,thread->cmds.data);
    cmds.length=strlen(cmds.data);

    send(new_sockfd,&cmds,sizeof(cmds.type)+sizeof(cmds.length)+cmds.length,0); 
   printf("%s\n",cmds.data);
   printf("type:%d\n",cmds.type);
    client_putsfile(new_sockfd,cmds.data);

}

void*threadFunc_gets(void*arg){
    thread_t*thread=(thread_t*)arg;
    //用新的netfd连接客户端
    int new_sockfd=connectTcp(thread->ip,thread->port);

    train_t cmds;
    cmds.type=USER_TOKEN;
    cmds.length=strlen(thread->username);
    strcpy(cmds.data,thread->username);
    //发送用户名
    send(new_sockfd,&cmds,sizeof(cmds.type)+sizeof(cmds.length)+cmds.length,0);

    bzero(&cmds, sizeof(train_t));
    char* token;
    //生成token
    cmds.type=USER_TOKEN;
    cmds.length=strlen(token);
    strcpy(cmds.data,token);
    //发送token
    send(new_sockfd,&cmds,sizeof(cmds.type)+sizeof(cmds.length)+cmds.length,0);

    //发送下载或上传命令
    send(new_sockfd,&thread->cmds,sizeof(thread->cmds.type)+sizeof(thread->cmds.length)+thread->cmds.length,0);
   printf("%s\n",thread->cmds.data);
    client_getsfile(new_sockfd,thread->cmds.data);


}

